<template>
  <div class="container">
    <div class="app-container">
      <div class="left">
        <el-input
          v-model="queryParams.keyword"
          style="margin-bottom: 10px"
          type="text"
          prefix-icon="el-icon-search"
          size="small"
          placeholder="输入员工姓名全员搜索"
          @input="input"
        />
        <!-- 树形组件 -->
        <el-tree
          ref="tree"
          :data="depts"
          :props="defaultProps"
          default-expand-all
          highlight-current
          :expand-on-click-node="false"
          node-key="id"
          @node-click="handleNodeClick"
          @current-change="changeCurrent"
        />
      </div>
      <div class="right">
        <el-row class="opeate-tools" type="flex" justify="end">
          <el-button size="mini" type="primary" @click="$router.push('/employee/detail')">添加员工</el-button>
          <el-button size="mini" @click="importExcel">excel导入</el-button>
          <el-button size="mini" @click="exportExcel">excel导出</el-button>
        </el-row>
        <!-- 表格组件 -->
        <el-table :data="tableData">
          <el-table-column align="center" label="头像" prop="staffPhoto">
            <template #default="{row}">
              <el-avatar v-if="row.staffPhoto" :src="row.staffPhoto" />
              <span v-else class="username">{{ row.username?.substring(0,1) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="手机号" sortable prop="mobile" />
          <el-table-column label="工号" sortable prop="workNumber" />
          <el-table-column label="聘用形式" prop="formOfEmployment">
            <template #default="{row}">
              <span>{{ row.formOfEmployment === 1? '正式' : '临时工' }}</span>
            </template>
          </el-table-column>
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职时间" sortable prop="timeOfEntry" />
          <el-table-column label="操作" width="280px">
            <template #default="{row}">
              <el-button size="mini" type="text" @click="$router.push(`/employee/detail/${row.id}`)">查看</el-button>
              <el-button size="mini" type="text" @click="btnRole(row.id)">角色</el-button>
              <el-popconfirm
                title="这是一段内容确定删除吗？"
                @onConfirm="delList(row.id)"
              >
                <el-button slot="reference" size="mini" type="text" style="margin-left:10px" @click="del(row.id)">删除</el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-row style="height: 60px" align="middle" type="flex" justify="end">
          <el-pagination
            layout="sizes,total,prev, pager, next"
            :total="total"
            :current-page="queryParams.page"
            :page-sizes="[1, 3, 5, 10]"
            :page-size="queryParams.pagesize"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </el-row>

        <el-dialog :visible.sync="showRoleDialog" title="分配角色">
          <!-- 弹层内容 -->
          <!-- checkbox -->
          <el-checkbox-group v-model="roleIds">
            <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">
              {{ item.name }}
            </el-checkbox>
          </el-checkbox-group>
          <el-row slot="footer" type="flex" justify="center">
            <el-col :span="6">
              <el-button type="primary" size="mini" @click="btnRoleOK">确定</el-button>
              <el-button size="mini" @click="showRoleDialog = false">取消</el-button>
            </el-col>
          </el-row>
        </el-dialog>

      </div>
    </div>
    <ImportExcelVue :show-excel-dialog.sync="showExcelDialog" />
  </div>
</template>
<script>
import FileSaver from 'file-saver'
import ImportExcelVue from './components/ImportExcel.vue'
import { getDepartment } from '@/api/department'
import { transListToTreeData } from '@/utils/index'
import { getEmployeeList, exportExcel, delEmployeeList, getRoleList, getEmployeeDetail, assignRoles } from '@/api/employee'
export default {
  name: 'Employee',
  components: {
    ImportExcelVue
  },
  data () {
    return {
      showExcelDialog: false,
      showRoleDialog: false,

      id: null,
      roleList: [], // 接收角色列表
      roleIds: [], // 用来双向绑定数据的
      depts: [
        {
          name: '传智教育',
          children: [
            { name: '总裁办' },
            { name: '行政部' },
            { name: '财务部' }
          ]
        }
      ],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      queryParams: {
        departmentId: null,
        page: 1,
        pagesize: 10,
        keyword: null
      },
      total: 0,
      tableData: [],
      timer: ''
    }
  },
  created () {
    this.getDepartmentData()
  },
  methods: {
    async getDepartmentData () {
      const data = await getDepartment()

      const res = transListToTreeData(data, 0)

      this.depts = res

      this.queryParams.departmentId = this.depts[0].id

      this.$nextTick(() => {
        this.$refs.tree.setCurrentKey(this.queryParams.departmentId)
      })
      this.getEmployeeData()
    },
    // 封装
    async getEmployeeData () {
      const data = await getEmployeeList(this.queryParams)

      this.total = data.total
      this.tableData = data.rows
    },
    handleNodeClick () {},
    changeCurrent (data) {
      this.queryParams.departmentId = data.id // 重新记录了参数
      this.getEmployeeData()
    },
    handleSizeChange (val) {
      this.queryParams.pagesize = val
      this.getEmployeeData()
    },
    handleCurrentChange (val) {
      this.queryParams.page = val
      this.getEmployeeData()
    },
    // 搜索
    input () {
      clearTimeout(this.timer)
      this.timer = setTimeout(() => {
        this.queryParams.page = 1
        this.getEmployeeData()
      }, 500)
    },
    // excel导出
    async exportExcel () {
      const data = await exportExcel()

      FileSaver.saveAs(data, '员工信息表.xlsx')
    },
    // 导入
    importExcel () {
      this.showExcelDialog = true
    },

    // 删除
    async delList (id) {
      console.log(1)
      await delEmployeeList(id)
      this.$message.success('删除成功')
      this.getDepartmentData()
    },

    // 分配角色弹窗
    async btnRole (id) {
      const data = await getRoleList()

      this.roleList = [...data]

      const res = await getEmployeeDetail(id)

      this.roleIds = res.roleIds

      this.id = id
      this.showRoleDialog = true
    },
    // 更新
    async  btnRoleOK () {
      await assignRoles({ id: this.id, roleIds: this.roleIds })

      this.$message.success('修改成功')
      this.showRoleDialog = false
    }

  }
}
</script>

<style lang="scss" scoped>
.app-container {
  background: #fff;
  display: flex;
  .left {
    width: 280px;
    padding: 20px;
    border-right: 1px solid #eaeef4;
  }
  .right {
    flex: 1;
    padding: 20px;
    .opeate-tools {
      margin: 10px;
    }
    .username {
      height: 30px;
      width: 30px;
      line-height: 30px;
      text-align: center;
      border-radius: 50%;
      color: #fff;
      background: #04c9be;
      font-size: 12px;
      display: inline-block;
    }
  }
}

</style>
